package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketOrder;
import com.cskaoyan.bean.PO.MarketOrderExample;
import com.cskaoyan.bean.VO.WxUserIndexInnerVO;
import com.cskaoyan.bean.VO.WxUserIndexVO;
import com.cskaoyan.mapper.MarketOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wenkaikai
 * @since 2022/08/18 09:34
 */
@Service
@Transactional
public class WxUserServiceImpl implements WxUserService{

    @Autowired
    MarketOrderMapper marketOrderMapper;

    /**
     * 未评价数量
     * 未付款数量 待付款
     * 未发货 代发货
     * 未收货 已发货
     * @param userId
     * @return
     */
    @Override
    public WxUserIndexVO queryMsgById(Integer userId) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        int uncomment = 0;
        int unpaid = 0;
        int unrecv = 0;
        int unship = 0;
        // 找出该用户下的所有订单
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        for (MarketOrder marketOrder : marketOrders) {
            // 根据订单status分类计算数量
            Short orderStatus = marketOrder.getOrderStatus();
            int value = orderStatus.intValue();
            if (value == 401 || value == 402) {
                // 用户已收货或系统收货，得到未评价的商品数量
                uncomment += marketOrder.getComments();
            }
            if(value == 101){
                unpaid++;
            }
            if (value == 201) {
                unship++;
            }
            if (value == 401) {
                unrecv++;
            }
        }
        WxUserIndexVO wxUserIndexVO = new WxUserIndexVO();
        WxUserIndexInnerVO wxUserIndexInnerVO = new WxUserIndexInnerVO();
        wxUserIndexInnerVO.setUncomment(uncomment);
        wxUserIndexInnerVO.setUnpaid(unpaid);
        wxUserIndexInnerVO.setUnrecv(unrecv);
        wxUserIndexInnerVO.setUnship(unship);
        wxUserIndexVO.setOrder(wxUserIndexInnerVO);
        return wxUserIndexVO;
    }
}